<?php


namespace App\Http\Controllers\admin\service;


use App\Http\Controllers\admin\model\AdminAppModel;
use App\Http\Controllers\admin\model\AdminModel;
use App\Http\Controllers\admin\model\AdminSnModel;
use App\Http\Controllers\admin\model\AppInstallModel;
use App\Http\Controllers\admin\model\AppTestModel;
use App\Http\Controllers\admin\model\AppUpdateModel;
use App\Http\Controllers\admin\model\BatchModel;
use App\Http\Controllers\admin\model\GradeModel;
use App\Http\Controllers\admin\model\ParentAppModel;
use App\Http\Controllers\admin\model\ParentSnModel;
use App\Http\Controllers\admin\model\SubjectModel;
use App\Http\Controllers\admin\model\TeacherAppModel;
use App\Http\Controllers\utils\LogUtil;
use App\Http\Controllers\utils\ToolUtil;
use App\Http\Controllers\utils\UploadUtil;
use Illuminate\Support\Facades\DB;

class AppService
{
    /**
     * 上传app应用
     * @param $param
     * @return false|string
     */
    public static function uploadApp($param){
        return ToolUtil::returnJson(302,'已弃用');
        $admin = AdminModel::queryLeftVip(['a.id'=>$param['userId']]);
        if($admin){
            if($admin['apk_num']>=($admin['vip_num']+$admin['upgrade'])){
                return ToolUtil::returnJson(301,'上传应用到达上限');
            }
            $param['size'] = $admin['vip_size']*1024*1024;
            return UploadUtil::uploadApk($param,$param['file']);
        }
        return ToolUtil::returnJson(302,'不存在该用户');
    }

    /**
     * 增加app信息
     * @param $param
     * @return false|string
     */
    public static function addApp($param){
        try {
            $admin = AdminModel::queryFirst(['id'=>$param['userId']],['group_id']);
            $param['remark'] = '增加APP信息';
            $data=[
                'app_name'=>$param['appName'],
                'app_version_name'=>$param['versionName'],
                'app_package'=>$param['pakName'],
                'app_version_code'=>$param['versionCode'],
                'app_size'=>$param['appSize'],
                'app_url'=>$param['appUrl'],
                'app_img'=>$param['appImg'],
                'create_time'=>time(),
                'admin_id'=>$param['userId'],
                'app_md5'=>$param['appMd5'],
                'app_score'=>$param['app_score']
            ];
            $param['statusType'] = '安装应用';
            DB::beginTransaction();

            if($param['type']==1){//管理员上传app信息

                $data['app_desc'] = $param['appDesc'];
                $data['app_grade_id'] = $param['gradeName'];
                $data['app_subject_id'] = $param['subjectName'];
                $data['app_type'] = $param['appType'];
                $data['app_status'] = $param['appStatus'];

                $data['app_preview1'] = $param['appPreview1'];
                $data['app_preview2'] = $param['appPreview2'];
                $data['app_preview3'] = $param['appPreview3'];
                $data['app_developer'] = $param['appDeveloper'];
                $data['app_remarks'] = $param['app_remarks'];

                if(AdminAppModel::add($data)){
                    $param['details'] = ToolUtil::handleSuccessOrError(['appName'=>$param['appName']]);
                    LogUtil::addAdminLog($param);
                    DB::commit();
                    return ToolUtil::returnJson(200,'成功');
                }
                return ToolUtil::returnJson(300,'失败');

            }elseif($param['type']==2){//老师上传app信息

                $bid = $param['batchId'];
                if(empty($bid)){
                    return ToolUtil::returnJson(300,'请选择班级');
                }
                $batchArr = explode(',',$bid);
                $batchId = [];
                $batchName = '';
                foreach ($batchArr as $key=>$value){
                    $res = BatchModel::queryFirst(['id'=>$value,'admin_id'=>$param['userId']],['id','name']);
                    if(!empty($res)){
                        $batchId[] = (string)$res['id'];
                        $batchName .= $res['name'].',';
                    }
                    
                }

                if(empty($batchId)){
                    return ToolUtil::returnJson(300,'班级错误');
                }

                foreach ($batchId as $key => $value) {
                    $app_batch = DB::table('t_teacher_app')->where(['app_package'=>$data['app_package']])
                        ->whereRaw(" find_in_set({$value},bind_batch_id) ")->first(['id']);

                    if(!empty($app_batch['id'])){
                        return ToolUtil::returnJson(301,'班级已有这个APP');
                    }
                }

                $batchValue = implode(',',$batchId);
                $data['bind_batch_id'] = $batchValue;
                $data['is_silent'] = $param['isSilent'];
                if(TeacherAppModel::add($data)){
                    DB::commit();
                    $param['details'] = ToolUtil::handleSuccessOrError(['appName'=>$param['appName'],'bName'=>$batchName]);
                    LogUtil::addAdminLog($param);
                    if($param['isSilent']){
                        $params['app_package'] = $param['pakName'];
                        $params['url'] = $param['appUrl'];
                        $param['type'] = 1;
                        $param['status'] = 1;
                        $param['contentType'] = 9;
                        $param['batch'] = $batchId;
                        $param['title'] = 'OK';
                        $jPush = LogUtil::addPushLog($param,$params);
                        if(in_array(200,$jPush)){
                            return ToolUtil::returnJson(200,'成功');
                        }
                        return ToolUtil::returnJson(200,'成功,推送失败');
                    }else{
                        return ToolUtil::returnJson(200,'成功');
                    }
                }
                return ToolUtil::returnJson(300,'失败');

            }elseif($param['type']==3){//家长上传app信息

                $sid = (int)$param['snId'];
                if(empty($sid)){
                    return ToolUtil::returnJson(300,'请选择sn');
                }
                $adminSn = AdminSnModel::queryFirst(['id'=>$sid],['value','id']);
                $parent = ParentSnModel::queryFirst(['admin_sn_id'=>$sid],['teacher_id']);
                if(empty($adminSn)){
                    return ToolUtil::returnJson(300,'sn错误');
                }
                if(empty($parent)){
                    return ToolUtil::returnJson(300,'家长sn错误');
                }
                if($parent['teacher_id']){
                    return ToolUtil::returnJson(302,'该设备授权给老师');
                }

                $p = ParentAppModel::queryFirst(['app_package'=>$data['app_package'],'sn_value'=>$adminSn['value']],['id']);
                if(!empty($p['id'])){
                    return ToolUtil::returnJson(302,'该设备已经有这个APP');
                }

                $data['sn_value'] = $adminSn['value'];
                $data['is_silent'] = $param['isSilent'];
                if(ParentAppModel::add($data)){
                    $param['details'] = ToolUtil::handleSuccessOrError(['appName'=>$param['appName'],'sn_value'=>$data['sn_value']]);
                    $apkNum = AdminModel::queryFirst(['id'=>$param['userId']],['apk_num'])['apk_num'];
                    AdminModel::edit(['id'=>$param['userId']],['apk_num'=>$apkNum+1]);
                    LogUtil::addAdminLog($param);
                    DB::commit();
                    if($param['isSilent']){
                        $params['app_package'] = $param['pakName'];
                        $params['url'] = $param['appUrl'];
                        $param['type'] = 2;
                        $param['status'] = 1;
                        $param['contentType'] = 9;
                        $param['title'] = 'OK';
                        $param['batch'] = array($adminSn['value']);
                        $jPush = LogUtil::addPushLog($param,$params);
                        if(in_array(200,$jPush)){
                            return ToolUtil::returnJson(200,'成功');
                        }
                        return ToolUtil::returnJson(200,'成功,推送失败');
                    }else{
                        return ToolUtil::returnJson(200,'成功');
                    }
                }
                return ToolUtil::returnJson(300,'失败');
            }else{
                return ToolUtil::returnJson(301,'没有权限');
            }
        }catch (\Exception $e){
            DB::rollBack();
            return ToolUtil::returnJson(500,$e->getMessage());
        }
    }


    /**
     * 编辑app信息
     * @param $param
     * @return false|string
     */
    public static function editApp($param){
        try {
            $data = [
                'app_name'=>$param['appName'],
                'app_version_name'=>$param['versionName'],
                'app_package'=>$param['pakName'],
                'app_version_code'=>$param['versionCode'],
                'app_size'=>$param['appSize'],
                'app_url'=>$param['appUrl'],
                'app_img'=>$param['appImg'],
                'update_time'=>time(),
                'app_md5'=>$param['appMd5'],
                'app_score'=>$param['app_score']
            ];
            $param['remark'] = '编辑app信息';
            DB::beginTransaction();
            if($param['type']==1){
                
                $data['app_preview1'] = $param['app_preview1'];
                $data['app_preview2'] = $param['app_preview2'];
                $data['app_preview3'] = $param['app_preview3'];
                $data['app_developer'] = $param['appDeveloper'];
                $data['app_desc'] = $param['appDesc'];
                $data['app_grade_id'] = $param['gradeName'];
                $data['app_subject_id'] = $param['subjectName'];
                $data['app_type'] = $param['appType'];
                $data['app_status'] = $param['appStatus'];
                $data['app_remarks'] = $param['app_remarks'];
                $int = AdminAppModel::edit(['id'=>$param['id']],$data);
                if($int){
                    DB::commit();
                    return ToolUtil::returnJson(200,'成功');
                }
                
            }elseif ($param['type']==2){

                $id = (int)$param['id'];
                $rec = DB::table('t_teacher_app')->where('id',$id)->first(['is_silent']);
                if(empty($rec)){
                    return ToolUtil::returnJson(300,'没有记录');
                }
                $bid = $param['batchId'];
                if(empty($bid)){
                    return ToolUtil::returnJson(300,'请选择班级');
                }
                $batchArr = explode(',',$bid);
                $batchId = [];
                $batchName = '';
                foreach ($batchArr as $key=>$value){
                    $res = BatchModel::queryFirst(['id'=>$value,'admin_id'=>$param['userId']],['id','name']);
                    if(!empty($res)){
                        $batchId[] = (string)$res['id'];
                        $batchName .= $res['name'].',';
                    }
                    
                }

                if(empty($batchId)){
                    return ToolUtil::returnJson(300,'班级错误');
                }

                foreach ($batchId as $key => $value) {
                    $app_batch = DB::table('t_teacher_app')
                        ->where(['app_package'=>$data['app_package']])
                        ->where('id', '<>', $param['id'])
                        ->whereRaw(" find_in_set({$value},bind_batch_id) ")
                        ->first(['id']);

                    if(!empty($app_batch['id'])){
                        return ToolUtil::returnJson(301,'班级已有这个APP');
                    }
                }

                $batchValue = implode(',',$batchId);
                $teacherApp = TeacherAppModel::queryFirst(['id'=>$param['id']],['bind_batch_id']);
                if(empty($teacherApp)){
                    return ToolUtil::returnJson(300,'没有这个app');
                }
                $batchArr2 = explode(',',$teacherApp['bind_batch_id']);

                $data['bind_batch_id'] = $batchValue;
                $data['is_silent'] = $param['isSilent'];

                $int = TeacherAppModel::edit(['id'=>$param['id']],$data);
                if($int){
                    $params['app_package'] = $param['pakName'];
                    $params['url'] = $data['app_url'];
                    $param['type'] = 1;
                    $param['status'] = 1;
                    $param['contentType'] = 10;
                    $param['title'] = 'OK'; 
                    DB::commit();
                    $batchIdArr3 = array_diff($batchArr2,$batchId);
                    if($batchIdArr3){
                        $param['statusType'] = '编辑app信息删除班级';
                        $param['remark'] = '编辑app信息删除班级';
                        $param['batch'] = $batchIdArr3;
                        LogUtil::addPushLog($param,$params);
                    }                    
                    if($param['isSilent'] || $rec['is_silent'] != $param['isSilent']){
                        $param['statusType'] = '编辑app信息';
                        $param['remark'] = '编辑app信息';                        
                        $param['batch'] = $batchId;
                        $param['contentType'] = 9;
                        $jPush = LogUtil::addPushLog($param,$params);
                        if(in_array(200,$jPush)){
                            return ToolUtil::returnJson(200,'成功');
                        }
                        return ToolUtil::returnJson(200,'成功,推送失败');
                    }
                    return ToolUtil::returnJson(200,'成功');
                }
            }elseif ($param['type']==3){

                $id = (int)$param['id'];
                $rec = DB::table('t_parent_app')->where('id',$id)->first(['sn_value','is_silent']);
                if(empty($rec)){
                    return ToolUtil::returnJson(300,'没有记录');
                }

                $sid = (int)$param['snId'];
                if(empty($sid)){
                    return ToolUtil::returnJson(300,'请选择sn');
                }
                $adminSn = AdminSnModel::queryFirst(['id'=>$sid],['value','id']);
                $parent = ParentSnModel::queryFirst(['admin_sn_id'=>$sid,'admin_id'=>$param['userId']],['teacher_id']);
                if(empty($adminSn)){
                    return ToolUtil::returnJson(300,'sn错误');
                }
                if(empty($parent)){
                    return ToolUtil::returnJson(300,'家长sn错误');
                }
                if($parent['teacher_id']){
                    return ToolUtil::returnJson(302,'该设备授权给老师');
                }

                $p = DB::table('t_parent_app')
                    ->where(['app_package'=>$data['app_package'],'sn_value'=>$adminSn['value']])
                    ->where('id', '<>', $param['id'])
                    ->first(['id']);
                if(!empty($p['id'])){
                    return ToolUtil::returnJson(302,'该设备已经有这个APP');
                }

                $data['sn_value'] = $adminSn['value'];
                $data['is_silent'] = $param['isSilent'];
                $int = ParentAppModel::edit(['id'=>$param['id']],$data);
                if($int){
                    $params['app_package'] = $param['pakName'];
                    $params['url'] = $data['app_url'];
                    $param['statusType'] = '编辑app信息';
                    DB::commit();
                    if($rec['sn_value'] != $adminSn['value']){

                        $push_data['app_package'] = $param['pakName'];
                        $param['statusType'] = '编辑app信息';
                        $param['type'] = 2;
                        $param['status'] = 1;
                        $param['contentType'] = 10;
                        $param['batch'] = array($rec['sn_value']);
                        $param['title'] = 'OK';
                        LogUtil::addPushLog($param,$push_data);

                    }

                    if($param['isSilent'] || $rec['is_silent'] != $param['isSilent']){
                        $param['type'] = 2;
                        $param['status'] = 1;
                        $param['contentType'] = 9;
                        $param['batch'] = array($data['sn_value']);
                        $param['title'] = 'OK';
                        $jPush = LogUtil::addPushLog($param,$params);
                        if(in_array(200,$jPush)){
                            return ToolUtil::returnJson(200,'成功');
                        }
                        return ToolUtil::returnJson(200,'成功,推送失败');
                    }
                    return ToolUtil::returnJson(200,'成功');
                }
            }else{
                return ToolUtil::returnJson(301,'没有权限');
            }
        }catch (\Exception $exception){
            DB::rollBack();
            return ToolUtil::returnJson(500,$exception->getMessage());
        }
        return ToolUtil::returnJson(300,'失败');
    }


    /**
     * 获取管理员app列表
     * @param $param
     * @return false|string
     */
    public static function queryAdminAppList($param){
        $where = [];
        if(isset($param['app_name']) && !empty($param['app_name'])){
            $where[0] = " a.app_name like '%$param[app_name]%' ";
        }
        if(isset($param['app_subject_id']) && !empty($param['app_subject_id'])){
            $where[count($where)+1] = " a.app_subject_id=$param[app_subject_id] ";
        }
        if(isset($param['gId']) && !empty($param['gId'])){
            $where[count($where)+1] = " a.app_grade_id=$param[gId] ";
        }
        $str = "";
        if($where){
            $str = ' where '.implode(' and ',$where);
        }
        $leftJoin = " left join t_subject as s on a.app_subject_id=s.id left join t_grade as g on a.app_grade_id=g.id";
        $count = DB::select("select count('*') as id from t_admin_app as a $leftJoin $str");
        $limit = ToolUtil::calculationPageTwo($param,$count[0]['id']);
        $field = "a.id,a.app_name,a.app_package,a.app_size,a.app_img,a.create_time,s.name,a.app_version_name,g.title,a.app_downloads,a.top_search,a.app_score";
        $data = DB::select("select $field from t_admin_app as a $leftJoin $str $limit");
        if(count($data)){
            return ToolUtil::returnJson(200,'成功',$data,$count[0]['id']);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 获取编号管理员app列表
     * @param $param
     * @return false|string
     */
    public static function queryIdAdminAppList($param){
        $field = ['a.id','a.app_name','a.app_package','a.app_size','a.app_img','g.title','s.name','a.app_downloads','a.app_preview1','a.app_preview2',
            'a.app_preview3','a.top_search','a.app_desc','a.create_time','a.app_score'];
        $data = DB::table('t_admin_app as a')
            ->leftJoin('t_grade as g','a.app_grade_id','=','g.id')
            ->leftJoin('t_subject as s','a.app_subject_id','=','s.id')
            ->where('a.id',$param['id'])
            ->first($field);
        $admin = DB::table('t_admin')->where('id',$param['userId'])->value('group_id');
        if(empty($admin)){
            return ToolUtil::returnJson(301,'不存在该用户');
        }
        switch ($admin){
            case 3:
                $equipment = DB::table('t_batch')->where('admin_id',$param['userId'])->get(['id','name']);
                break;
            case 4:
                $equipment = DB::table('t_parent_sn as p')
                    ->leftJoin('t_admin_sn as a','p.admin_sn_id','=','a.id')
                    ->where('admin_id',$param['userId'])->get(['a.id','a.name']);
                break;
            default:
                $equipment = [];
                break;
        }
        if($data){
            $data['equipment'] = $equipment;
            return ToolUtil::returnJson(200,'成功',$data);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 应用市场自动安装
     * @param $param
     * @return false|string
     */
    public static function installAdminApp($param){
        try {
            $param['remark'] = '应用市场安装';
            DB::beginTransaction();
            $appField = ['app_name','app_package','app_version_name','app_version_code','app_size','app_url','app_img','app_md5','app_score',
                'app_preview1','app_preview2','app_preview3','app_developer'];
            $app = DB::table('t_admin_app')->where('id',$param['appId'])->first($appField);
            if(empty($app)){
                return ToolUtil::returnJson(301,'不存在该应用');
            }
            $app['create_time'] = time();
            $app['is_silent'] = (int)$param['is_silent'];
            $app['admin_id'] = (int)$param['userId'];
            $admin = DB::table('t_admin')->where('id',$param['userId'])->value('group_id');
            if(empty($admin)){
                return ToolUtil::returnJson(302,'不存在该用户');
            }
            
            switch ($admin){
                case 3:
                    $batch = explode(',',$param['snId']);
                    foreach ($batch as $key=>$value){

                        $res = DB::table('t_teacher_app')
                        ->whereRaw(" find_in_set('{$value}',bind_batch_id) ")
                        ->where(['app_package'=>$app['app_package'],'app_version_name'=>$app['app_version_name']])
                        ->value('id');
                        if($res){
                            return ToolUtil::returnJson(301,'有批次重复安装了');
                        }
                    }

                    $app['bind_batch_id'] = $param['snId'];
                    $int = DB::table('t_teacher_app')->insert($app);
                    break;
                case 4:
                    $int = false;
                    $batch = [];
                    $sn_data = explode(',',$param['snId']);
                    foreach ($sn_data as $key=>$value){
                        $app['sn_value'] = DB::table('t_admin_sn')->where('id',$value)->value('value');
                        if(!empty($app['sn_value'])){
                            $batch[] = $app['sn_value'];
                        }
                        $res = DB::table('t_parent_app')->where(['sn_value'=>$app['sn_value'],'app_package'=>$app['app_package'],'app_version_name'=>$app['app_version_name']])->value('id');
                        if($res){
                            return ToolUtil::returnJson(301,'不能重复安装');
                        }
                        $int = DB::table('t_parent_app')->insert($app);
                    }
                    break;
                default:
                    $batch = [];
                    $int = false;
            }
            
            if($int){
                $param['statusType'] = '应用市场安装';
                $params['package'] = $app['app_package'];
                $params['url'] = $app['app_url'];
                if($admin==3){
                    $param['type'] = 1;
                }else{
                    $param['type'] = 2;
                }
                $param['status'] = 1;
                $param['contentType'] = 9;
                $param['title'] = 'OK';
                $param['batch'] = $batch;
                $param['details'] = '包名:'.$app['app_package'].',应用名:'.$app['app_name'];
                LogUtil::addAdminLog($param);
                DB::commit();
                if($app['is_silent'] === 1){
                    $jPush = LogUtil::addPushLog($param,$params);
                    if(in_array(200,$jPush)){
                        return ToolUtil::returnJson(200,'成功');
                    }
                    return ToolUtil::returnJson(200,'成功,推送失败');
                }else{
                    return ToolUtil::returnJson(200,'成功');
                }

            }
            DB::rollBack();
            return ToolUtil::returnJson(300,'安装失败');
        }catch (\Exception $exception){
            DB::rollBack();
            return ToolUtil::returnJson(500,'安装错误');
        }
    }

    /**
     * 获取年级分类名称
     * @return false|string
     */
    public static function getGradeName(){
        $data = DB::table('t_grade')->get(['id','title']);
        if(count($data)){
            return ToolUtil::returnJson(200,'成功',$data);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 删除app信息
     * @param $param
     * @return false|string
     */
    public static function deleteApp($param){
        try {
            $param['remark'] = '删除app信息';
            $param['statusType'] = '删除app信息';
            DB::beginTransaction();
            $admin = AdminModel::queryFirst(['id'=>$param['userId']],['apk_num']);
            $apk_num = $admin['apk_num']-1;
            if($apk_num < 0){
                $apk_num = 0;
            }
            if($param['type']==1){
                $app = AdminAppModel::queryFirst(['id'=>$param['id']],['app_package']);
                if(AdminAppModel::delete(['id'=>$param['id']])){
                    DB::commit();
                    $param['details'] = ToolUtil::handleSuccessOrError(['appName'=>$app['app_package']]);
                    LogUtil::addAdminLog($param);
                    AdminModel::edit(['id'=>$param['userId']],['apk_num'=>$apk_num]);
                    return ToolUtil::returnJson(200,'成功');
                }
                DB::rollBack();
                return ToolUtil::returnJson(300,'失败');
            }elseif($param['type']==2){
                $app = TeacherAppModel::queryFirst(['id'=>$param['id']],['app_package','is_silent','bind_batch_id','admin_id']);
                if(TeacherAppModel::delete(['id'=>$param['id']])){
                    $param['details'] = ToolUtil::handleSuccessOrError(['appName'=>$app['app_package']]);
                    LogUtil::addAdminLog($param);
                    AdminModel::edit(['id'=>$app['admin_id']],['apk_num'=>$apk_num]);
                    
                    DB::commit();
                    $params['app_package'] = $app['app_package'];
                    $param['type'] = 1;
                    $param['status'] = 1;
                    $param['contentType'] = 10;
                    $param['batch'] = explode(',',$app['bind_batch_id']);
                    $param['title'] = 'OK';
                    $jPush = LogUtil::addPushLog($param,$params);
                    if(in_array(200,$jPush)){
                        return ToolUtil::returnJson(200,'成功');
                    }
                    return ToolUtil::returnJson(200,'成功,推送失败');

                }
                DB::rollBack();
                return ToolUtil::returnJson(300,'失败');
            }elseif($param['type']==3){
                $app = ParentAppModel::queryFirst(['id'=>$param['id']],['app_package','is_silent','sn_value','admin_id']);
                if(ParentAppModel::delete(['id'=>$param['id']])){
                    $param['details'] = ToolUtil::handleSuccessOrError(['appName'=>$app['app_package']]);
                    LogUtil::addAdminLog($param);
                    AdminModel::edit(['id'=>$app['admin_id']],['apk_num'=>$apk_num]);
                    DB::commit();
                    $params['app_package'] = $app['app_package'];
                    $param['type'] = 2;
                    $param['status'] = 1;
                    $param['contentType'] = 10;
                    $param['batch'] = array($app['sn_value']);
                    $param['title'] = 'OK';
                    $jPush = LogUtil::addPushLog($param,$params);

                    $t_app_inside = DB::table('t_app_inside')->where('app_id',$param['id'])->where('type',1)->delete();
                    if($t_app_inside){
                        $param['contentType'] = 7;
                        $param['type'] = 2;
                        $param['batch'] = array($app['sn_value']);
                        $param['statusType'] = '后台删除应用ID管控';
                        LogUtil::addPushLog($param);
                        $param['remark'] = '后台删除应用ID管控';
                        $param['details'] = ToolUtil::handleSuccessOrError(['sn'=>$app['sn_value']]);
                        LogUtil::addAdminLog($param);    
                    }
                    
                    if(in_array(200,$jPush)){
                        return ToolUtil::returnJson(200,'成功');
                    }
                    return ToolUtil::returnJson(200,'成功,推送失败');
                    
                }
                DB::rollBack();
                return ToolUtil::returnJson(300,'失败');
            }
        }catch (\Exception $exception){
            DB::rollBack();
            return ToolUtil::returnJson(500,'网络拥堵,稍后再试');
        }
    }

    /**
     * 编号获取app信息
     * @param $param
     * @return false|string
     */
    public static function queryIdApp($param){
        if($param['type']==1){
            $app = AdminAppModel::queryLeftFirst(['a.id'=>$param['id']]);
            if($app){
                return ToolUtil::returnJson(200,'成功',$app);
            }
            return  ToolUtil::returnJson(400,'没有数据');
        }elseif ($param['type']==2){
            $field = ['t.id','t.app_name','t.app_version_name','t.app_package','t.app_version_code','t.app_size','t.app_url',
                't.app_img','a.username','t.update_time','t.is_update','t.is_silent','t.bind_batch_id','t.create_time','t.app_md5','t.app_score'];
            $app = TeacherAppModel::queryLeftFirst(['t.id'=>$param['id']],$field);
            if($app){
                $batchArr = explode(',',$app['bind_batch_id']);
                $batchNameArr = [];
                foreach ($batchArr as $k=>$v){
                    $batchNameArr[$k] = BatchModel::whereField(['id'=>$v,'admin_id'=>$param['userId']],'name');
                }
                $batchName = implode(',',$batchNameArr);
                $app['bind_batch_name'] = $batchName;
                return ToolUtil::returnJson(200,'成功',$app);
            }
            return  ToolUtil::returnJson(400,'没有数据');
        }elseif ($param['type']==3){
            $field = ['t.id','t.app_name','t.app_version_name','t.app_package','t.app_version_code','t.app_size','t.app_url',
                't.app_img','a.username','t.update_time','t.is_update','t.is_silent','t.sn_value','t.create_time','t.app_md5',
                'as.name','t.app_score','as.id as snId'];
            $app = ParentAppModel::queryLeftFirst(['t.id'=>$param['id']],$field);
            if($app){
                return ToolUtil::returnJson(200,'成功',$app);
            }
            return  ToolUtil::returnJson(400,'没有数据');
        }else{
            return  ToolUtil::returnJson(400,'没有数据');
        }
    }


    /**
     * 获取管理员app信息
     * @param $param
     * @return false|string
     */
    public static function queryAdminApp($param){
        switch ($param['type']){
            case 1:
                $data = self::handleQueryAdminApp([],$param);
                break;
            case 2:
                $param['field'] = 'a.app_name';
                $param['a.app_name'] = $param['appName'];
                $data = self::handleQueryAdminApp([],$param);
                break;
            case 3:
                $param['field'] = 'a.app_package';
                $param['a.app_package'] = $param['pakName'];
                $data = self::handleQueryAdminApp([],$param);
                break;
            default:
                $data = self::handleQueryAdminApp([],$param);
                break;
        }
        if(count($data['data'])){
            return ToolUtil::returnJson(200,'成功',$data['data'],$data['count']);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 处理管理员app信息
     * @param array $where
     * @param $param
     * @return mixed
     */
    public static function handleQueryAdminApp($where=[],$param){
        if($where){
            $data['count'] = AdminAppModel::queryCount($where,$param);
            $page = ToolUtil::calculationPage($param,$data['count']);
            $data['data'] = AdminAppModel::queryAll($where,$param,$page);
        }else{
            $data['count'] = AdminAppModel::queryCount([],$param);
            $page = ToolUtil::calculationPage($param,$data['count']);
            $data['data'] = AdminAppModel::queryAll([],$param,$page);
        }
        return $data;
    }

    /**
     * 获取老师app信息
     * @param $param
     * @return false|string
     */
    public static function queryTeacherApp($param){
        $admin = AdminModel::queryFirst(['id'=>$param['userId']],['group_id']);
        $field = ['t.id','t.app_name','t.app_version_name','t.app_package','t.app_version_code','t.app_size','t.app_url',
            't.app_img','t.create_time','a.username','t.is_silent','t.bind_batch_id','t.update_time'];
        if($admin['group_id']==1 || $admin['group_id']==2){
            switch ($param['type']){
                case 1:
                    $data = self::handleQueryTeacherApp([],$param,$field);
                    break;
                case 2:
                    $param['field'] = 't.app_name';
                    $param['t.app_name'] = $param['appName'];
                    $data = self::handleQueryTeacherApp([],$param,$field);
                    break;
                case 3:
                    $param['field'] = 't.app_package';
                    $param['t.app_package'] = $param['pakName'];
                    $data = self::handleQueryTeacherApp([],$param,$field);
                    break;
                default:
                    $param['field'] = 'a.username';
                    $param['a.username'] = $param['username'];
                    $data = self::handleQueryTeacherApp([],$param,$field);//
                    break;
            }
        }else{
            $where['t.admin_id'] = $param['userId'];
            switch ($param['type']){
                case 1:
                    $data = self::handleQueryTeacherApp($where,$param,$field);
                    break;
                case 2:
                    $param['field'] = 't.app_name';
                    $param['t.app_name'] = $param['appName'];
                    $data = self::handleQueryTeacherApp($where,$param,$field);
                    break;
                case 3:
                    $param['field'] = 't.app_package';
                    $param['t.app_package'] = $param['pakName'];
                    $data = self::handleQueryTeacherApp($where,$param,$field);
                    break;
                default:
                    $param['field'] = 'a.username';
                    $param['a.username'] = $param['username'];
                    $data = self::handleQueryTeacherApp($where,$param,$field);//
                    break;
            }
        }
        if(count($data['data'])){
            $batchNameArr = [];
            foreach ($data['data'] as $k=>$v){
                $batchName = '';
                $batchArr = explode(',',$v['bind_batch_id']);
                foreach ($batchArr as $key=>$value){
                    $batchNameArr[$key] = BatchModel::whereField(['id'=>$value],'name');
                }
                foreach ($batchNameArr as $key=>$value){
                    if(count($batchNameArr)==1){
                        $batchName .= $value;
                    }elseif (count($batchNameArr)-1==$key){
                        $batchName .= $value;
                    }else{
                        $batchName .= $value.',';
                    }
                }
                $v['bind_batch_id'] = $batchName;
                $data['data'][$k] = $v;
            }
            return ToolUtil::returnJson(200,'成功',$data['data'],$data['count']);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 处理老师app应用信息
     * @param array $where
     * @param $param
     * @param array $field
     * @return mixed
     */
    public static function handleQueryTeacherApp($where=[],$param,$field){
        if($where){
            $data['count'] = TeacherAppModel::queryCount($where,$param);
            $page = ToolUtil::calculationPage($param,$data['count']);
            $data['data'] = TeacherAppModel::queryAll($where,$param,$page,$field);
        }else{
            $data['count'] = TeacherAppModel::queryCount([],$param);
            $page = ToolUtil::calculationPage($param,$data['count']);
            $data['data'] = TeacherAppModel::queryAll([],$param,$page,$field);
        }
        return $data;
    }

    /**
     * 获取家长app信息
     * @param $param
     * @return false|string
     */
    public static function queryParentApp($param){
        $admin = AdminModel::queryFirst(['id'=>$param['userId']],['group_id']);
        $field = ['t.id','t.app_name','t.app_version_name','t.app_package','t.app_version_code','t.app_size','t.app_url',
            't.app_img','a.username','t.update_time','t.is_update','t.is_silent','t.sn_value','t.create_time',
            'sn.name as snName'];
        if($admin['group_id']==1 || $admin['group_id']==2){
            switch ($param['type']){
                case 1:
                    $data = self::handleQueryParentApp([],$param,$field);
                    break;
                case 2:
                    $param['field'] = 't.app_name';
                    $param['t.app_name'] = $param['appName'];
                    $data = self::handleQueryParentApp([],$param,$field);
                    break;
                case 3:
                    $param['field'] = 't.app_package';
                    $param['t.app_package'] = $param['pakName'];
                    $data = self::handleQueryParentApp([],$param,$field);
                    break;
                default:
                    $param['field'] = 'a.username';
                    $param['a.username'] = $param['username'];
                    $data = self::handleQueryParentApp([],$param,$field);//
                    break;
            }
        }else{
            $where['t.admin_id'] = $param['userId'];
            switch ($param['type']){
                case 1:
                    $data = self::handleQueryParentApp($where,$param,$field);
                    break;
                case 2:
                    $param['field'] = 't.app_name';
                    $param['t.app_name'] = $param['appName'];
                    $data = self::handleQueryParentApp($where,$param,$field);
                    break;
                case 3:
                    $param['field'] = 't.app_package';
                    $param['t.app_package'] = $param['pakName'];
                    $data = self::handleQueryParentApp($where,$param,$field);
                    break;
                default:
                    $param['field'] = 'a.username';
                    $param['a.username'] = $param['username'];
                    $data = self::handleQueryParentApp($where,$param,$field);//
                    break;
            }
        }
        if(count($data['data'])){
            return ToolUtil::returnJson(200,'成功',$data['data'],$data['count']);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 处理家长app信息
     * @param array $where
     * @param $param
     * @param array $field
     * @return mixed
     */
    public static function handleQueryParentApp($where=[],$param,$field){
        if($where){
            $data['count'] = ParentAppModel::queryCount($where,$param);
            $page = ToolUtil::calculationPage($param,$data['count']);
            $data['data'] = ParentAppModel::queryAll($where,$param,$page,$field);
        }else{
            $data['count'] = ParentAppModel::queryCount([],$param);
            $page = ToolUtil::calculationPage($param,$data['count']);
            $data['data'] = ParentAppModel::queryAll([],$param,$page,$field);
        }
        return $data;
    }

    /**
     * 获取批次名称
     * @param $param
     * @return false|string
     */
    public static function queryBatchName($param){
        $batch = BatchModel::queryGet(['admin_id'=>$param['userId']],['id','name']);
        if(count($batch)){
            return ToolUtil::returnJson(200,'成功',$batch);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 获取班级名称
     * @param $param
     * @return false|string
     */
    public static function queryGradeName($param){
        $grade = GradeModel::queryGet([],['id','title']);
        if(count($grade)){
            return ToolUtil::returnJson(200,'成功',$grade);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 上传预览图片
     * @param $param
     * @return false|string
     */
    public static function uploadPreviewImg($param){
        $param['size'] = 100000000*1024*1024;
        $param['width'] = 8000;
        $param['height'] = 8000;
        return UploadUtil::uploadImg($param,$param['file']);
    }

    /**
     * @param $param
     * 获取科目名称
     * @return false|string
     */
    public static function querySubjectName($param){
        $where = [];
        if(isset($param['gId']) && !empty($param['gId'])){
            $where['s.grade_id'] = $param['gId'];
        }
        $data = DB::table('t_subject as s')
            ->leftJoin('t_grade as g','s.grade_id','=','g.id')
            ->where($where)
            ->get(['s.id','s.name']);
        if(count($data)){
            return ToolUtil::returnJson(200,'成功',$data);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 增加应用全局更新
     * @param $param
     * @return false|string
     */
    public static function addAppUpdate($param){

        try{

            $pg_type = LogUtil::getPackageType($param['packageName']);
            if(!$pg_type){
                return ToolUtil::returnJson(301,'不存在该包名信息');
            }

            $param['remark'] = '增加应用全局更新';
            $param['details'] = ToolUtil::handleSuccessOrError(['package'=>$param['packageName']]);
            if($param['status']==1){
                $batch = "MTK";
            }elseif ($param['status']==2){
                $batch = "展锐";
            }elseif ($param['status']==3){
                $batch = "全平台";
            }else{
                return ToolUtil::returnJson(300,'平台类型错误');
            }
            $data = [
                'app_name'=>$param['appName'],
                'app_package'=>$param['packageName'],
                'app_version_name'=>$param['versionName'],
                'app_version_code'=>$param['versionCode'],
                'app_size'=>$param['appSize'],
                'app_desc'=>$param['appDesc'],
                'app_md5'=>$param['appMd5'],
                'app_url'=>$param['appUrl'],
                'create_time'=>time(),
                'status'=>$param['status']
            ];
            $int = AppUpdateModel::add($data);
            $params =[
                'app_name'=>$param['appName'],
                'package'=>$param['packageName'],
                'url'=>$param['appUrl']
            ];
            
            if($int){
                $param['type'] = 1;
                $param['contentType'] = 9;
                $param['statusType'] = '增加应用全局更新';
                $param['batch'] = array($batch);
                LogUtil::addAdminLog($param);
                $jPush = LogUtil::addPushLog($param,$params,$pg_type);
                if(in_array(200,$jPush)){
                    return ToolUtil::returnJson(200,'成功');
                }
                return ToolUtil::returnJson(200,'成功,推送失败');
            }

        }catch (\Exception $exception){
            
            return ToolUtil::returnJson(500,$exception->getMessage());
        }

        return ToolUtil::returnJson(300,'失败');
    }

    /**
     * 获取应用全局更新
     * @param $param
     * @return false|string
     */
    public static function queryAppUpdate($param){
        switch ($param['type']){
            case 1:
                $data = self::handleQueryAppUpdate($param);
                break;
            case 2:
                $param['field'] = 'app_name';
                $param['app_name'] = $param['appName'];
                $data = self::handleQueryAppUpdate($param);
                break;
            case 3:
                $param['field'] = 'app_package';
                $param['app_package'] = $param['packageName'];
                $data = self::handleQueryAppUpdate($param);
                break;
            default:
                $data['data'] = null;
        }
        if(count($data['data'])){
            return ToolUtil::returnJson(200,'成功',$data['data'],$data['count']);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 处理获取应用全局更新
     * @param $param
     * @return mixed
     */
    public static function handleQueryAppUpdate($param){
        $data['count'] = AppUpdateModel::queryCount([],$param);
        $page = ToolUtil::calculationPage($param,$data['count']);
        $data['data'] = AppUpdateModel::queryAll([],$param,$page);
        return $data;
    }

    /**
     * 编号获取应用全局更新
     * @param $param
     * @return false|string
     */
    public static function queryIdAppUpdate($param){
        $field = ['id','app_name','app_package','app_version_name','app_version_code','app_size','app_desc','app_url','app_md5','status'];
        $data = AppUpdateModel::queryFirst(['id'=>$param['id']],$field);
        if($data){
            return ToolUtil::returnJson(200,'成功',$data);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 编辑应用全局更新
     * @param $param
     * @return false|string
     */
    public static function updateAppUpdate($param){
        $param['remark'] = '更新应用全局更新';
        $param['details'] = ToolUtil::handleSuccessOrError(['package_name'=>$param['packageName']]);
        $data = [
            'app_name'=>$param['appName'],
            'app_package'=>$param['packageName'],
            'app_version_name'=>$param['versionName'],
            'app_version_code'=>$param['versionCode'],
            'app_size'=>$param['appSize'],
            'app_desc'=>$param['appDesc'],
            'app_md5'=>$param['appMd5'],
            'app_url'=>$param['appUrl'],
            'update_time'=>time()
        ];
        $int = AppUpdateModel::edit(['id'=>$param['id']],$data);
        if($int){
            LogUtil::addAdminLog($param);
            return ToolUtil::returnJson(200,'成功');
        }
        return ToolUtil::returnJson(300,'失败');
    }

    /**
     * 删除应用全局更新
     * @param $param
     * @return false|string
     */
    public static function deleteAppUpdate($param){
        $appUpdate = AppUpdateModel::queryFirst(['id'=>$param['id']],['app_package']);
        if($appUpdate){
            $param['remark'] = '删除应用全局更新';
            $param['details'] = ToolUtil::handleSuccessOrError(['package_name'=>$appUpdate['app_package']]);
            $int = AppUpdateModel::delete(['id'=>$param['id']]);
            if($int){
                LogUtil::addAdminLog($param);
                return ToolUtil::returnJson(200,'成功');
            }
            return ToolUtil::returnJson(300,'失败');
        }
        return ToolUtil::returnJson(301,'不存在该信息');
    }

    /**
     * 增加灰度测试
     * @param $param
     * @return false|string
     * @throws \PHPExcel_Exception
     * @throws \PHPExcel_Reader_Exception
     */
    public static function addAppTest($param){
        if(!ToolUtil::checkExcelSuffix($param)){
            return ToolUtil::returnJson(302,'文件类型必须为xlsx或xls');
        }
        $phpReader = new \PHPExcel_Reader_Excel2007();
        $phpExcel = $phpReader->load($param['file']);
        $currentSheet = $phpExcel->getSheet(0);//获取第一个表单
        $allRow = $currentSheet->getHighestRow();//获取总行数
        $maxColumn = $currentSheet->getHighestColumn();//获取最大列号
        $allColumn=\PHPExcel_Cell::columnIndexFromString($maxColumn);//获取总列数
        $snArr = [];
        for ($currentRow=2;$currentRow<=$allRow;$currentRow++){
            for ($currentColumn=0;$currentColumn<$allColumn;$currentColumn++){
                $val = trim($currentSheet->getCellByColumnAndRow($currentColumn,$currentRow)->getValue());
                if($val){
                    $snArr[$currentRow-2] = $val;
                }
                break;
            }
        }
        if(!$snArr){
            return ToolUtil::returnJson(301,'不存在sn码');
        }
        $param['remark'] = '增加灰度测试';
        $param['details'] = ToolUtil::handleSuccessOrError(['package'=>$param['packageName']]);
        $data = [
            'app_name'=>$param['appName'],
            'app_package'=>$param['packageName'],
            'app_version_name'=>$param['versionName'],
            'app_version_code'=>$param['versionCode'],
            'app_size'=>$param['appSize'],
            'app_desc'=>$param['appDesc'],
            'app_md5'=>$param['appMd5'],
            'app_url'=>$param['appUrl'],
            'sn'=>implode(',',$snArr),
            'create_time'=>time()
        ];
        $int = AppTestModel::add($data);
        $params =[
            'app_name'=>$param['appName'],
            'package'=>$param['packageName'],
            'url'=>$param['appUrl']
        ];
        if($int){
            $param['type'] = 2;
            $param['status'] = 1;
            $param['contentType'] = 9;
            $param['statusType'] = '增加灰度测试';
            $param['batch'] = $snArr;
            LogUtil::addAdminLog($param);
            $jPush = LogUtil::addPushLog($param,$params);
            if(in_array(200,$jPush)){
                return ToolUtil::returnJson(200,'成功');
            }
            return ToolUtil::returnJson(200,'成功,推送失败');
        }
        return ToolUtil::returnJson(300,'失败');
    }

    /**
     * 获取灰度测试信息
     * @param $param
     * @return false|string
     */
    public static function queryAppTest($param){
        switch ($param['type']){
            case 1:
                $data = self::handleQueryAppTest($param);
                break;
            case 2:
                $param['field'] = 'app_name';
                $param['app_name'] = $param['appName'];
                $data = self::handleQueryAppTest($param);
                break;
            case 3:
                $param['field'] = 'app_package';
                $param['app_package'] = $param['packageName'];
                $data = self::handleQueryAppTest($param);
                break;
            default:
                $data['data'] = null;
        }
        if(count($data['data'])){
            return ToolUtil::returnJson(200,'成功',$data['data'],$data['count']);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 处理获取灰度测试
     * @param $param
     * @return mixed
     */
    public static function handleQueryAppTest($param){
        $data['count'] = AppTestModel::queryCount([],$param);
        $page = ToolUtil::calculationPage($param,$data['count']);
        $data['data'] = AppTestModel::queryAll([],$param,$page);
        return $data;
    }

    /**
     * 删除灰度测试
     * @param $param
     * @return false|string
     */
    public static function deleteAppTest($param){
        $appTest = AppTestModel::queryFirst(['id'=>$param['id']],['app_package']);
        if($appTest){
            $param['remark'] = '删除灰度测试';
            $param['details'] = ToolUtil::handleSuccessOrError(['package_name'=>$appTest['app_package']]);
            $int = AppTestModel::delete(['id'=>$param['id']]);
            if($int){
                LogUtil::addAdminLog($param);
                return ToolUtil::returnJson(200,'成功');
            }
            return ToolUtil::returnJson(300,'失败');
        }
        return ToolUtil::returnJson(301,'不存在该信息');
    }

    /**
     * 包名获取安装的sn信息
     * @param $param
     * @return false|string
     */
    public static function packageNameQuerySn($param){
        $admin = AdminModel::queryFirst(['id'=>$param['userId']],['group_id']);
        if($admin['group_id']==1 || $admin['group_id']==2){
            $parentSn = ParentSnModel::queryLeftGet([],['a.value','p.teacher_id','admin_id']);
            if(count($parentSn)){
                $data = self::handlePackageNameQuerySn($parentSn,$param);
            }else{
                $data = [];
            }
        }else{
            $parentSn = ParentSnModel::queryLeftGet(['p.admin_id'=>$param['userId']],['a.value','p.teacher_id','admin_id']);
            if(count($parentSn)){
                $data = self::handlePackageNameQuerySn($parentSn,$param);
            }else{
                $data = [];
            }
        }
        if($data){
            return ToolUtil::returnJson(200,'成功',$data);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 处理包名获取安装的sn信息
     * @param $parentSn
     * @param $param
     * @return array
     */
    public static function handlePackageNameQuerySn($parentSn,$param){
        $arr = [];
        foreach ($parentSn as $k=>$v){
            if($v['teacher_id']){
                $teacher = TeacherAppModel::queryFirst(['app_package'=>$param['packageName']],['id']);
                if($teacher){
                    $app = AppInstallModel::queryFirst(['sn_value'=>$v['value'],'teacher_app_id'=>$teacher['id']],['sn_value','create_time']);
                    if($app){
                        $app['username'] = AdminModel::queryFirst(['id'=>$v['admin_id']],['username'])['username'];
                        $arr[$k] = $app;
                    }
                }
            }else{
                $teacher = ParentAppModel::queryFirst(['app_package'=>$param['packageName']],['id']);
                if($teacher){
                    $app = AppInstallModel::queryFirst(['sn_value'=>$v['value'],'parent_app_id'=>$teacher['id']],['sn_value','create_time']);
                    if($app){
                        $app['username'] = AdminModel::queryFirst(['id'=>$v['admin_id']],['username'])['username'];
                        $arr[$k] = $app;
                    }
                }
            }
        }
        return $arr;
    }

    /**
     * 增加系统应用
     * @param $param
     * @return false|string
     */
    public static function addSysApp($param){
        $param['remark'] = '增加系统应用';
        $data = [
            'app_name'=>$param['app_name'],
            'app_package'=>$param['app_package'],
            'app_img'=>$param['app_img'],
            'create_time'=>time()
        ];
        $id = DB::table('t_sys_app')->where(['app_package'=>$param['app_package']])->value('id');
        if($id){
            return ToolUtil::returnJson(301,'存在相同包名');
        }
        if(DB::table('t_sys_app')->insert($data)){
            $param['details'] = ToolUtil::handleSuccessOrError($param);
            LogUtil::addAdminLog($param);
            return ToolUtil::returnJson(200,'成功');
        }
        return ToolUtil::returnJson(300,'失败');
    }

    /**
     * 获取系统应用
     * @param $param
     * @return false|string
     */
    public static function querySysApp($param){
        $where = [];
        if(isset($param['app_name']) && !empty($param['app_name'])){
            $where[0] = " app_name like '%$param[app_name]%' ";
        }
        if(isset($param['app_package']) && !empty($param['app_package'])){
            $where[0] = " app_package like '%$param[app_package]%' ";
        }
        $str = '';
        if($where){
            $str = ' where '.implode('and',$where);
        }
        $count = DB::select("select count('*') as id from t_sys_app $str");
        $field = "id,app_name,app_package,app_img,create_time";
        $limit = ToolUtil::calculationPageTwo($param,$count[0]['id']);
        $data = DB::select("select $field from t_sys_app $str $limit");
        if(count($data)){
            return ToolUtil::returnJson(200,'成功',$data,$count[0]['id']);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 编号获取系统应用
     * @param $param
     * @return false|string
     */
    public static function queryIdSysApp($param){
        $field = ['id','app_name','app_package','app_img'];
        $data = DB::table('t_sys_app')->where('id',$param['id'])->first($field);
        if($data){
            return ToolUtil::returnJson(200,'成功',$data);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 修改系统应用
     * @param $param
     * @return false|string
     */
    public static function editSysApp($param){
        $param['remark'] = '修改系统应用';
        $data = [
            'app_name'=>$param['app_name'],
            'app_package'=>$param['app_package'],
            'app_img'=>$param['app_img'],
            'update_time'=>time()
        ];
        $id = DB::table('t_sys_app')->where(['app_package'=>$param['app_package']])->value('id');
        if($id){
            if($param['id']!=$id){
                return ToolUtil::returnJson(301,'存在相同包名');
            }
        }
        if(DB::table('t_sys_app')->where('id',$param['id'])->update($data)){
            $param['details'] = ToolUtil::handleSuccessOrError($param);
            LogUtil::addAdminLog($param);
            return ToolUtil::returnJson(200,'成功');
        }
        return ToolUtil::returnJson(300,'失败');
    }

    /**
     * 删除系统应用
     * @param $param
     * @return false|string
     */
    public static function deleteSysApp($param){
        $param['remark'] = '删除系统应用';
        $field = ['id','app_name','app_package','app_img'];
        $sysApp = DB::table('t_sys_app')->where('id',$param['id'])->first($field);
        if(empty($sysApp)){
            return ToolUtil::returnJson(301,'不存在该应用');
        }
        if(DB::table('t_sys_app')->where('id',$param['id'])->delete()){
            $param['details'] = ToolUtil::handleSuccessOrError($sysApp);
            LogUtil::addAdminLog($param);
            return ToolUtil::returnJson(200,'成功');
        }
        return ToolUtil::returnJson(300,'失败');
    }
}